$(document).ready(function(){
var swiper = new Swiper('.swiper-container', {
    setWrapperSize :true,
    direction:"vertical",
    speed : 500,
    longSwipesRatio : 0.3,
	   noSwiping : true,
     noSwipingClass:'swiper-no-swiping',
    grabCursor:true,
    mousewheelControl : true,
    lazyLoading : true,
    lazyLoadingInPrevNext : true,
    lazyLoadingOnTransitionStart : true,

    onInit: function(swiper){
    },
    onSlideChangeEnd: function(swiper){
      if(swiper.activeIndex == 1){
       $(".next-arrow").hide();
       video.getPlayer().pause();
      }else{
        $(".next-arrow").show();
      }
    }
});

//audio();
  function audio(){
    var audio = $("#musicAudio").get(0);
    var audioOn = true;
    audio.play();
    $(".audio").click(function(){
      if(audioOn){
        audio.pause();
        $(".audio-play").hide();
        $(".audio-pause").show();
        audioOn = false;
      }else{
        audio.play();
        $(".audio-play").show();
        $(".audio-pause").hide();
        audioOn = true;
      }
    });
  }


//opener door

	(function door(){
    setTimeout(function(){
      $("#door-left").animate({left:"-50%"}, 600);
      $("#door-right").animate({right:"-50%"}, 800);
      setTimeout(function(){
        $("#door-streamer").animate({top:"-50%"},1100);
      },600);
      setTimeout(function(){
        $(".couplet,#door-streamer").hide();
      },1000);
      $(".next-arrow").show();
    },3000)
	})();

//video
var video = new WxMoment.Video({
    //请联系接口人确认视频清晰度已调至高清版本
    //如果要定制“播放按钮”的样式，请使用 CSS 覆盖 .tvp_overlay_play 和 .tvp_button_play 即可

    vid: "i03673nbahs", //视频 vid 取自视频地址：http://v.qq.com/page/a/t/t/a0016gys8ct.html
    pic: "./img/cover-video.jpg", //设置视频默认缩略图
    oninited: function () {
        //播放器在视频载入完毕触发
    },
    onplaying: function () {
        //播放器真正开始播放视频第一帧画面时

    },
    onpause: function () {
        //播放器触发暂停时，目前只针对HTML5播放器有效
    },
    onresume: function () {
        //暂停后继续播放时触发
    },
    onallended: function () {
        //播放器播放完毕时

    },
    onfullscreen: function (isfull) {
        //onfullscreen(isfull) 播放器触发全屏/非全屏时，参数isfull表示当前是否是全屏
    }
  });

//可以通过以下方式控制播放/暂停
//video.getPlayer().play();
//video.getPlayer().pause();

//以下可以拉起iOS全屏播放
//video.getPlayer().enterFullScreen();

var
  $w = $(window).width(),
  $h = $(window).height(),
  $topVal1 = $w,
  $topVal2 = $w,
  $cloud1 = $(".cloud1"),
  $cloud2 = $(".cloud2"),
  $cover = $(".cover"),
  $doorBoxBtn = $(".door-box-btn"),
  $carBox = $(".car-box"),
  $doorBox = $(".door-box"),
  $boxBox = $(".box-box"),
  $doorBtn = $(".door-btn"),
  $boxBtn = $(".box-btn"),
  $carDoorIcon =$(".car-door-icon"),
  $carBoxIcon = $(".car-box-icon"),
  $page2_bg = $(".page2"),
  $time,
  $planNum = 0;

  if($w >= 540){
    $w = 540 - 50;
  }

(function charge(){
  $(".progress-box").css('width',$w - 50);
})();


//背景图移动

function bg(){
    $time = setInterval(function(){
      $planNum++;
      // console.log(num);
      $page2_bg.css("backgroundPosition",$planNum+"%");

      if($planNum === 25){
        clearInterval($time);
        $cover.fadeIn(200);
        $doorBoxBtn.show();
      };
      if($planNum === 45){
        clearInterval($time);
        $cover.fadeIn(200);
        $boxBox.show();
      };
      if($planNum === 90){
        $cloud1.fadeOut();
        $cloud2.fadeOut();
      }
      if($planNum === 100){
        clearInterval($time);
        $(".next-arrow").fadeOut();
        $doorBoxBtn.fadeOut();
        $carBox.fadeOut(200);
        $(".turnplate-box,.rule").show();
      };
    },70);
}

//加装车门
$(".door-box").one("touchstart",".door-btn",function(){
  $(".car-door-icon").animate({"top":"7","left":"109"},1000,function(){
    $(".car-door-icon").animate({"top":"5","left":"103"},200);
    setTimeout(function(){
      $doorBox.hide();
      $cover.hide();
      bg();
    },500);

  });
});
//加装箱体
$(".box-box").one("touchstart",".box-btn",function(){
  $(".car-box-icon").animate({"top":"-6","left":"20"},1000,function(){
    $(".car-box-icon").animate({"top":"-9","left":"18"},200);
    setTimeout(function(){
      $doorBoxBtn.hide();
      $cover.hide();
      bg();
    },500);
  });
});

//背景云彩


function clound1 (){
  setInterval(function(){
    $topVal1--;
    // console.log($topVal1);
    $cloud1.css({"left":$topVal1});
    if( $topVal1 == -180){
      $cloud1.css({"left":$w + 100});
      $topVal1 = $w;
    }
  },5);
}
function clound2 (){
  setInterval(function(){
    $topVal2--;
    // console.log($topVal2);
    $cloud2.css({"left":$topVal2});
    if( $topVal2 == -230){
      $cloud2.css({"left":$w + 150});
      $topVal2 = $w;
    }
  },10);
}

//规则
$(".rule").click(function(){
  $(".popup-rule").show();
  $cover.show();
});
$(".popup-rule .del").click(function(){
  $(".popup-rule").hide();
  $cover.hide();
})


//旋转转盘及对应弹框部分

//旋转次数
  var rotNum = 0;
  //旋转角度
  var angles;
  //可抽奖次数
  var clickNum = 1;

  $('#beginBtn').on('click',function(){

   if (clickNum >= 1) {
     $(this).addClass('turnplatw-btn-active');
     //可抽奖次数减一
     clickNum = clickNum-1;
     //转盘旋转
     runCup();
     //转盘旋转过程“开始抽奖”按钮无法点击
     $('#tupBtn').attr("disabled", true);

     //“开始抽奖”按钮无法点击恢复点击
     setTimeout(function(){
       $('#tupBtn').removeClass('turnplatw-btn-active');
       $('#tupBtn').removeAttr("disabled", true);
       $("#prizeNo1").show();
       $cover.show();
     },6000);
   }
   else{
     $('#popWin_bg').show();
     $('.dialog-noAward').show();
   }
  });

  //转盘旋转
  function runCup(){
    //旋转次数加一
    rotNum = rotNum + 1;
    angles = -(4320* rotNum);
    var degValue = 'rotate('+angles+'deg'+')';
    $('#myCanvas').css('-webkit-transform',degValue);      //Chrome和Safari
    $('#myCanvas').css('transform',degValue);
  }


//抽开云
  $("#tupBtn").on("touchstart",function(){
    $(this).addClass("turnplatw-btn-hover");
    $cover.show();
    $("#userInfo").show();
  });


  $(".popup input[type='button']").click(function(){
    $(".popup").hide();
    $cover.hide();
  });

  $(".voucher").click(function(){
    $cover.show();
    $(".popup-mini-voucher").show();
  })

  $(".btn2").click(function(){
    $(".popup-mini-voucher").hide();
    $cover.hide();
  });

  //充电

(function(){
  var
    $__num =1,
    t;
  var fn = function(){
    $__num += 5;
    $__num = $__num > 100 ? 100 : $__num;
    $(".charge .progress-bar-color").css("width",$__num+'%');
    $(".charge .num").text(parseInt($__num)+"%");

    if($__num == 100) {
      if(t) clearInterval(t);
      $cover.fadeOut();
      $(".charge").hide();
      $(".car-box .wheel").addClass("wheel-turn");
      bg();
      clound1 ();
      clound2 ();
    }

  };
  $(document).on("touchstart touchend",".charge .charge-btn",function(e){
    e.preventDefault();
    if(e.type == 'touchstart'){
      if(t) clearInterval(t);
      t = setInterval(fn, 100);
    } else {
      clearInterval(t);
    }
  })
})();


});
